package tongcheng.com.tongcheng.base.main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yanzhenjie.nohttp.RequestMethod;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.Setting;
import tongcheng.com.tongcheng.utils.ShowToast;

public class XiuGaiUsernameActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.title_bar)
    RelativeLayout mBg;
    @Bind(R.id.et_gengXinContent)
    EditText et_gengXinContent;
    @Bind(R.id.bt_gengXin)
    Button bt_gengXin;
    private int ss = 0;
    @Override
    public Integer initResid() {
        return R.layout.activity_xiu_gai_username;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {
        settitlebar(mBg);

        //mTitle.setTextColor(Color.WHITE);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        Intent mIntent = getIntent();
        String text = mIntent.getStringExtra("text");
        mTitle.setText(text);
        if(text.equals("修改用户名")){
            ss = 1;
            et_gengXinContent.setHint("请输入新用户名");
            et_gengXinContent.setText(Setting.getUserName()+"");
        }else if(text.equals("修改手机号码")){
            ss = 2;
            et_gengXinContent.setHint("请输入新手机号码");
            et_gengXinContent.setText(Setting.getPhone()+"");
        }else if(text.equals("修改详细地址")){
            ss = 3;
            et_gengXinContent.setHint("请输入详细地址");
            et_gengXinContent.setText(Setting.getAddress_detail());
        }


        bt_gengXin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String string = et_gengXinContent.getText().toString().trim();
                if(TextUtils.isEmpty(string)){
                    ShowToast.showToast("更新信息不能为空");
                }else {
                    xiuGaiUsername(string);
                }
            }
        });
    }

    private void xiuGaiUsername(final String string) {
        MyRequest request = new MyRequest(ConStans.GengXinGeRenXinXi_Url, RequestMethod.POST);
        request.add("userid", Setting.getId());
        if(ss == 1){
            request.add("user_name",string);
        }else if(ss == 2){
            request.add("phone",string);
        }else if(ss == 3){
            request.add("address_detail",string);
        }

        CallServer.getInstance().addPost(this, request, 32, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                Log.e("修改用户名",json);
                try {
                    JSONObject object = new JSONObject(json);
                    if(object.getInt("status") == 1){
                        if(ss == 1){
                            Setting.setUserName(string);
                        }else if(ss == 2){
                            Setting.setPhone(string);
                        }else if(ss == 3){
                            Setting.setAddress_detail(string);
                        }
//                        ShowToast.showToast(object.getString("msg"));
                        finish();
                    }else {
                        ShowToast.showToast(object.getString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
